﻿// Copyright richyp 2009
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Richyp.Bodger.UI {
    public class BodgerFile {
        public void Save(string filename, CodexFile codex, List<DataGridViewRow> entries) {
            StreamWriter writer = new StreamWriter(filename);            
            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            writer.WriteLine("<bodgerFile version=\"rp2\">");
            writer.WriteLine("  <header>");
            writer.WriteLine("    <codex>" + codex.Name + "</codex>");
            writer.WriteLine("    <userListName>" + codex.UserListName + "</userListName>");
            writer.WriteLine("    <size>0</size>");
            writer.WriteLine("    <name>foo</name>");
            writer.WriteLine("  </header>");
            writer.WriteLine("  <list>");
            foreach (DataGridViewRow row in entries) {
                if (row.Tag is ListEntry) {
                    ListEntry entry = (ListEntry)row.Tag;
                    writer.WriteLine("    <entry>");
                    writer.WriteLine("      <name>" + entry.BaseEntry.Name + "</name>");
                    writer.WriteLine("      <units>");
                    foreach (Unit unit in entry.Units) {
                        writer.WriteLine("        <unit>");
                        writer.WriteLine("          <name>" + unit.Name + "</name>");
                        writer.WriteLine("          <options>");
                        foreach (Equipment equipment in unit.Equipped) {
                            writer.WriteLine("            <equipment>" + equipment.Wargear.Name + "</equipment>");
                        }
                        writer.WriteLine("          </options>");
                        writer.WriteLine("        </unit>");
                    }
                    writer.WriteLine("      </units>");
                    writer.WriteLine("    </entry>");
                }
            }
            writer.WriteLine("  </list>");
            writer.WriteLine("</bodgerFile>");
            writer.Flush();
            writer.Close();
        }

        public void Load(string filename, DataGridView list,MainWindow window) {
            XmlDocument doc = new XmlDocument();
            list.Rows.Clear();
            doc.Load(filename);
            XmlNode codexNode = doc.GetElementsByTagName("codex")[0];
            XmlNode userListNameNode = doc.GetElementsByTagName("userListName")[0];
            CodexParser parser = new CodexParser();
            this.CodexFile = (CodexFile)parser.Parse("data/codex/" + codexNode.InnerText);
            if(userListNameNode != null)
            {
                this.CodexFile.UserListName = userListNameNode.InnerText;
            }
            window.CodexFile = this.CodexFile;
            window.PopulateCodexEntries(true);
            foreach (XmlNode node in doc.GetElementsByTagName("entry")) {
                CodexEntry entry = null;
                ListEntry listEntry = null;
                foreach (XmlNode child in node.ChildNodes) {
                    if (child.Name.ToUpper() == "NAME") {
                        try {
                            entry = CodexFile.GetEntryByName(child.InnerText);
                        } catch {
                            throw new Exception("Could not find entry: " + child.InnerText + " in codex " + CodexFile.Name);
                        }
                        listEntry = new ListEntry(entry, list);
                        listEntry.Units.Clear();
                    } else if (child.Name.ToUpper() == "UNITS") {
                        foreach (XmlNode unitsNode in child.ChildNodes) {
                            if (unitsNode.Name.ToUpper() == "UNIT") {
                                Unit unitBase = null;
                                foreach (XmlNode unitNode in unitsNode.ChildNodes) {
                                    if (unitNode.Name.ToUpper() == "NAME") {
                                        foreach (Unit unit in entry.Units) {
                                            if (unit.Name == unitNode.InnerText) {
                                                unitBase = Unit.Copy(unit);
                                                break;
                                            }
                                        }
                                    } else if (unitNode.Name.ToUpper() == "OPTIONS") {
                                        unitBase.Equipped.Clear();
                                        foreach (XmlNode itemNode in unitNode.ChildNodes) {
                                            if (itemNode.Name.ToUpper() == "EQUIPMENT") {
                                                foreach (Equipment equipment in unitBase.AllWargear) {
                                                    if (equipment.Wargear.Name == itemNode.InnerText) {
                                                        unitBase.Equipped.Add(equipment);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                }
                                listEntry.Units.Add(unitBase);
                            }
                        }
                        
                    }
                }
                int offset = 1;
                int position = -1;
                foreach (DataGridViewRow row in list.Rows) {
                    if (row is CategoryRow) {
                        string category = ((CategoryRow)row).Category;
                        if (position > -1 && category != entry.Category) {
                            position = offset - 1;
                            break;
                        }
                        if (category == entry.Category) {
                            position = offset;
                        }
                    }
                    offset++;
                }
                list.Rows.Insert(position, listEntry.Row);
                listEntry.UpdateRow();
            }
            window.ValidateList();
        }

        public CodexFile CodexFile { get; set; }

        private List<Unit> units = new List<Unit>();
        public List<Unit> Units {
            get {
                return this.units;
            }
        }
    }
}
